package blackjack.client.appletVisualAndLogic;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import blackjack.client.ConnectionApplet;

/**
 * painting getCard
 */
@SuppressWarnings("serial")
public class GameLogic extends JPanel implements ActionListener{
	ConnectionApplet client;

	String winner = "WINNER !!!";
	String looser = "LOOSER";
	String draw = "NOBODY WON";
	
	Font f = new Font(null, Font.ITALIC, 15);
	FontMetrics fontMetrics = getFontMetrics(new Font(null, Font.ITALIC, 20));
	
	int looserWidth = fontMetrics.stringWidth(looser);
	int winnerWidth = fontMetrics.stringWidth(winner);
	int drawWidth = fontMetrics.stringWidth(draw);
	
	Image myCards;
	Image backgr;

	public GameLogic(ConnectionApplet client){
		this.client = client;	
	}

	public void actionPerformed(ActionEvent arg0) {
		client.newGame.setEnabled(false);
		if(client.getPoints() < 21) client.getout().println("getCard");
		Graphics g = getGraphics();
		paintComponent(g);
	}

	/*SELLEKS ET Nupud OLID ILUSAD*/
	public void imageDisain(){
		ImageIcon newGameImg1 = new ImageIcon("images/newgame1.jpg");
		ImageIcon enough1 = new ImageIcon("images/enough1.jpg");
		ImageIcon quit1 = new ImageIcon("images/quit1.jpg");
		ImageIcon getcards1 = new ImageIcon("images/getcards1.jpg");
		client.newGame.setBackground(Color.BLACK);
		client.enough.setBackground(Color.BLACK);
		client.quit.setBackground(Color.BLACK);
		client.getCard.setBackground(Color.BLACK);
		client.newGame.setIcon(newGameImg1);
		client.enough.setIcon(enough1);
		client.quit.setIcon(quit1);
		client.getCard.setIcon(getcards1);

		client.enough.setIcon(enough1);
		client.quit.setIcon(quit1);
		client.getCard.setIcon(getcards1);	
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		backgr = Toolkit.getDefaultToolkit().getImage ("images/background.jpg");

		imageDisain();
		g.drawImage(backgr, 0, 0, getWidth(), getHeight(),null);
		drawPoints(g);

		int kokku1 = 0;
		int kokku2 = 0;
		
		for(int y = 0; y < client.getPickedCards().size(); y++){//vaatab et kaarrt on voetud vastavalt sellel joonistab
			myCards =  Toolkit.getDefaultToolkit().getImage ("images/" + client.getPickedCards().get(y) + ".jpg");
			g.drawImage(myCards, y*60, getHeight()/2, this);
		}

		if(client.getFirst() != null) {
			FontMetrics fontMetrics = getFontMetrics(f);
			int width = fontMetrics.stringWidth(client.getFirst());

			g.setFont(f);
			g.setColor(Color.CYAN);
			g.drawString(client.getFirst() , getWidth() - width, 15);//kaardi votja
		}

		for(int z = 0; z < client.getEsimeneKard().size(); z++){
			if(client.getParam() != 0){
				kokku1 += client.getDeck().CardStringtoInt((String) 
						client.getEsimeneKard().get(z));//s4itaet o4ki sopernika
			}
		}
		if(client.getParam() != 0){
			FontMetrics fontMetrics = getFontMetrics(f);
			String kokku = (Integer.toString(kokku1));
			int width = fontMetrics.stringWidth(kokku + "-");
			int width1 = fontMetrics.stringWidth(client.getFirst());
			g.setFont(f);
			g.setColor(Color.CYAN);
			g.drawString(kokku + "-", getWidth() - width - width1, 15);
		}

		for(int z = 0; z < client.getTeineKard().size(); z++){
			if(client.getParam1() != 0){
				kokku2 += client.getDeck().CardStringtoInt((String) client.getTeineKard().get(z));
			}
		}
		if(client.getParam1() != 0){
			FontMetrics fontMetrics = getFontMetrics(f);
			int width = fontMetrics.stringWidth(Integer.toString(kokku2)+ "-" + client.getSecond());
			g.drawString((Integer.toString(kokku2)+ "-" + client.getSecond()), getWidth() - width, 30);
		}

		if(client.getEsimeneSuri() != 0 && client.getTeineSuri() != 0){
			drawWinner(g);
			client.getCard().setEnabled(false);
			client.getEnough().setEnabled(false);
			client.newGame.setEnabled(true);
		}

		if((client.getParam() != 0 || client.getParam1() != 0) ){
			//draw winner, looser or draw
			if(client.getPoints() > 21){
				if(kokku1 > 21 && kokku2 > 21){
					drawDraw(g);
				}else drawLooser(g);
			}
			
			if(client.getPoints() <= 21){
				if((kokku1 > client.getPoints() && kokku1 <= 21) || (kokku2 > client.getPoints() && kokku2 <= 21)){
					drawLooser(g);
				}else drawWinner(g);
			}
		}
	}

	/**
	 * draws looser, winner or draw
	 * @param g
	 */
	private void drawLooser(Graphics g) {
		g.drawString(looser, getWidth() - looserWidth, getHeight()/3);
	}

	private void drawWinner(Graphics g) {
		g.drawString(winner, getWidth() - winnerWidth, getHeight()/3);
	}

	private void drawDraw(Graphics g) {
		g.drawString(draw, getWidth() - drawWidth, getHeight()/3);
	}

	/**
	 * draws currentPlayer's points
	 * @param g
	 */
	private void drawPoints(Graphics g) {
		int height = 18;
		Font f = new Font(null, Font.ITALIC, height);
		g.setFont(f);
		g.setColor(Color.CYAN);
		g.drawString("Points : " + Integer.toString(client.getPoints()), 0, getHeight()/2 - height);
	}
}
